package listbyorder.access201_300.test214;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/8/29 14:25
 */
public class Solution1 {

    public String shortestPalindrome(String s) {
        if (s == null || s.equals("") || s.length() == 1) return s;
        char[] cs = s.toCharArray();
        int maxH = 0;
        for (int i = 0; i < cs.length; i++) {
            if (isHuiwen(cs, 0, i)) {
                maxH = i + 1;
            }
        }
        if (maxH == cs.length) return s;
        StringBuilder pre = new StringBuilder(s.substring(maxH));
        pre.reverse();
        pre.append(s);
        return pre.toString();
    }

    // 分行去计算newStr的情况
    private String help(char[] cs, int n) {
        int len = cs.length;
        for (int i = n; i >= 0; i--) {
            if (isHuiwen(cs, i, len - 1)) {
                StringBuilder res = new StringBuilder();
                for (int j = i; j < len; j++) {
                    res.append(cs[j]);
                }
                return res.toString();
            }
        }
        return null;
    }

    private boolean isHuiwen(char[] cs, int l, int r) {
        while (l < r) {
            if (cs[l++] != cs[r--]) return false;
        }
        return true;
    }
}
